package com.zh.flowable.domain.flow.listener;//package com.zh.simple.domain.flow.listener;
//
//import com.zh.simple.support.SpringContextHolder;
//import lombok.AllArgsConstructor;
//import org.flowable.engine.IdentityService;
//import org.flowable.engine.TaskService;
//import org.flowable.engine.delegate.DelegateExecution;
//import org.flowable.engine.delegate.ExecutionListener;
//import org.flowable.idm.api.User;
//import org.flowable.task.api.Task;
//import org.springframework.stereotype.Component;
//
//@AllArgsConstructor
//@Component
//public class ManagerAssignmentListener implements ExecutionListener {
//    private IdentityService identityService;
//    private TaskService taskService;
//
//    static String groupId = "managers";
//    @Override
//    public void notify(DelegateExecution execution) {
//        System.out.println("[debug] ManagerAssignmentListener notify: EventName="+execution.getEventName()+", CurrentActivityName="+execution.getCurrentActivityName());
//        //申请人userId
//        String applicant = (String) execution.getVariable("applicant");
//        System.out.println("[debug] notify: applicant UserId="+applicant);
//
//        execution.getProcessInstanceId();
//        // 2. 查询当前任务
//        Task task = taskService
//                .createTaskQuery()
//                .processInstanceId(execution.getProcessInstanceId())
//                .singleResult();
//
//        // 3. 自动分配任务给指定HR
//        if (task != null) {
//            var users = identityService.createUserQuery().memberOfGroup(groupId).list();
//            if (users.isEmpty()){
//                System.err.println("group "+groupId+"中找不到users");
//                return;
//            }
//            User first = users.get(0);
//
//            taskService.setAssignee(task.getId(), first.getId());
//            System.out.println("HR任务自动分配给: " + first.getId());
//        } else{
//            System.err.println("找不到任务  ProcessInstanceId="+execution.getProcessInstanceId());
//        }
//    }
//}
